package cn.bocweb.spinning.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.ErrorBean;
import cn.bocweb.spinning.model.Bean.RegVerifyBean;
import cn.bocweb.spinning.model.Bean.UserBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.common.BaseActivity;
import cn.bocweb.spinning.view.common.RegVerifyButton;
import cn.bocweb.spinning.view.fragment.RegisterNoticeFragment;
import de.greenrobot.event.EventBus;

public class RegisterActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.reg_nickname)
    EditText regNickname;
    @Bind(R.id.reg_pwd)
    EditText regPwd;
    @Bind(R.id.reg_confirm_pwd)
    EditText regConfirmPwd;
    @Bind(R.id.reg_phone)
    EditText regPhone;
    @Bind(R.id.reg_verify)
    EditText regVerify;
    @Bind(R.id.reg_btn_verify)
    RegVerifyButton regBtnVerify;
    @Bind(R.id.reg_btn_submit)
    Button regBtnSubmit;
    @Bind(R.id.reg_btn_login)
    TextView regBtnLogin;
    @Bind(R.id.reg_check)
    CheckBox regCheck;
    @Bind(R.id.reg_protocol)
    TextView regProtocol;

    UserPresenter presenter;
    RegisterNoticeFragment registerNoticeFragment;

    int toRegisterState;
    boolean isVerifyClickable = true;
    boolean isRegClickable = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);

        EventBus.getDefault().register(this);

        presenter = new UserPresenterImp();
        registerNoticeFragment = new RegisterNoticeFragment();

        initVerrify(savedInstanceState);
        initClickBtn();
        initToolbar();
        initIntent();
        regCheck.setChecked(true);
    }

    private void initIntent() {
        Intent intent = getIntent();
        toRegisterState = intent.getIntExtra(Constant.TO_REGISTER, 0);
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    private void initClickBtn() {
        regBtnVerify.setOnClickListener(this);
        regBtnLogin.setOnClickListener(this);
        regBtnSubmit.setOnClickListener(this);
        regProtocol.setOnClickListener(this);
    }

    private void initVerrify(Bundle savedInstanceState) {
        regBtnVerify.onCreate(savedInstanceState);
        regBtnVerify.setTextAfter("秒", this).setTextBefore("获取验证码", this).setLenght(60 * 1000);
    }

    private void initToolbar() {
        toolbar.setTitle("注册");
        toolbar.setTitleTextColor(getResources().getColor(R.color.white));
        toolbar.setNavigationIcon(R.mipmap.btn_exit);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.reg_btn_login: {
                finish();
                break;
            }
            case R.id.reg_protocol: {
                registerNoticeFragment.show(getSupportFragmentManager(), "registerDoc");
                break;
            }
            case R.id.reg_btn_verify: {
                if (isVerifyClickable) {
                    if (TextUtils.isEmpty(regPhone.getText().toString())) {
                        DeviceUtil.MyToast(this, "请输入手机号码...");
                        if (dialog.isShowing()) {
                            dialog.dismiss();
                        }
                        break;
                    }
                    if (!(regPhone.getText().toString().subSequence(0, 1).equals("1") && regPhone.getText().toString().length() == 11)) {
                        DeviceUtil.MyToast(this, "请输入正确的手机号...");
                        if (dialog.isShowing()) {
                            dialog.dismiss();
                        }
                        break;
                    }
                    presenter.regVerify(regPhone.getText().toString());
                    isVerifyClickable = false;
                }
                break;
            }
            case R.id.reg_btn_submit: {
                if (isRegClickable) {
                    if (regCheck.isChecked()) {
                        isRegClickable = false;
                        btnReg();
                    } else {
                        DeviceUtil.MyToast(RegisterActivity.this, Constant.PRESENTER_PROTOCOL);
                    }
                }
                break;
            }
        }
    }

    private void btnReg() {
        showLoading();
        String nickname = regNickname.getText().toString();
        String phone = regPhone.getText().toString();
        String verify = regVerify.getText().toString();
        String pwd = regPwd.getText().toString();
        String pwdconfirm = regConfirmPwd.getText().toString();
        if (TextUtils.isEmpty(nickname)) {
            DeviceUtil.MyToast(this, "请输入昵称...");
            isRegClickable = true;
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
            return;
        }
        if (TextUtils.isEmpty(pwd)) {
            DeviceUtil.MyToast(this, "请输入密码...");
            isRegClickable = true;

            if (dialog.isShowing()) {
                dialog.dismiss();
            }
            return;
        }
        if (TextUtils.isEmpty(phone)) {
            DeviceUtil.MyToast(this, "请输入手机号码...");
            isRegClickable = true;

            if (dialog.isShowing()) {
                dialog.dismiss();
            }
            return;
        }
        if (TextUtils.isEmpty(verify)) {
            DeviceUtil.MyToast(this, "请输入验证码...");
            isRegClickable = true;

            if (dialog.isShowing()) {
                dialog.dismiss();
            }
            return;
        }

        if (!(phone.subSequence(0, 1).equals("1") && phone.length() == 11)) {
            DeviceUtil.MyToast(this, "请输入正确的手机号...");
            isRegClickable = true;

            if (dialog.isShowing()) {
                dialog.dismiss();
            }
            return;
        }
        if (!pwd.equals(pwdconfirm)) {
            DeviceUtil.MyToast(this, "两次输入的密码不一致哦...");
            isRegClickable = true;

            if (dialog.isShowing()) {
                dialog.dismiss();
            }
            return;
        }
        presenter.reg(nickname, pwd, pwdconfirm, phone, verify);
    }


    /**
     * 验证码接口回调
     */
    public void onEventMainThread(RegVerifyBean bean) {
        DeviceUtil.MyToast(this, bean.getMsg());
        if (bean.getReturn_code() == 0) {
            regBtnVerify.startTime();
        } else {
            regPhone.setText("");
        }
        isVerifyClickable = true;
    }

    /**
     * 注册接口回调
     */

    public void onEventMainThread(UserBean bean) {

        DeviceUtil.MyToast(this, bean.getMsg());
        if (bean.getReturn_code() == 0) {
            UserBean.DataEntity user = bean.getData();
            user.writeInfoIntoLocal(this);

            switch (toRegisterState) {
                case Constant.TRAIN_TO_LOGIN: {
                    setResult(Constant.TRAIN_TO_LOGIN);
                    finish();
                    break;
                }
                case Constant.SEQUENCE_TEST_COLLECT: {
                    setResult(Constant.SEQUENCE_TEST_COLLECT);
                    finish();
                    break;
                }
                case Constant.SEQUENCE_TEST_SYNC: {
                    setResult(Constant.SEQUENCE_TEST_SYNC);
                    finish();
                    break;
                }
                case Constant.INFODETAIL_TO_LOGIN: {
                    setResult(Constant.LOGIN_TO_MAIN);
                    finish();
                    break;
                }
                default: {
                    finish();
                }
            }
        }
        isRegClickable = true;
        hideLoading();
    }


    public void onEventMainThread(ErrorBean bean) {
        DeviceUtil.MyToast(this, bean.getErrorMsg());
        hideLoading();
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }
}
